<template>
    <div>
     <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
         <!-- 卡片列表 -->
         <el-card>
           <!-- 添加搜索区域 -->
           <el-row :gutter="20">
             <el-col :span="8">
                <el-input placeholder="请输入内容" v-model="queryData.query" class="input-with-select" clearable @clear="getGoods">
                   <el-button @click="getGoods" slot="append" icon="el-icon-search"></el-button>
                </el-input>
             </el-col>
             <el-col :span="4">
               <el-button type="primary" @click="$router.push('/goods/add')">添加商品</el-button>
             </el-col>
           </el-row>
           <!-- 表格列表 -->
            <el-table
            :data="goodsList"
            style="width: 100%" border>
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column
              prop="goods_name"
              label="商品名称" width="300px"></el-table-column>
            <el-table-column
              prop="goods_price"
              label="商品价格(元)"></el-table-column>
            <el-table-column
              prop="goods_weight"
              label="商品重量"></el-table-column>
            <el-table-column
              label="创建时间">
              <template slot-scope="scope">
              {{scope.row.add_time | formatData}}
              </template>
            </el-table-column>
            <el-table-column
              label="操作" width="150px">
              <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
                <el-button type="warning" icon="el-icon-delete" size="mini" @click="removeGoods(scope.row.goods_id)"></el-button>
              </template>
            </el-table-column>
            </el-table>
            <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryData.pagenum"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
         </el-card>
    </div>
</template>

<script>

export default {
  data() {
    return {
      queryData:{
        query:"",
        pagesize:10,
        pagenum:1
      },
      goodsList:[],
      total:0
    }
  },
  
  created() {
    this.getGoods();
  },

  methods: {
    async getGoods(){
      const {data:res}=await this.$axios.get('goods',{params:this.queryData})
       if(res.meta.status!=200) return this.$message.error('获取数据失败')
       this.goodsList=res.data.goods
       this.total=res.data.total
    },
    // 每页条数发生变化
    handleSizeChange(val){
      this.queryData.pagesize=val;
      this.getGoods()
    },
    // 页码发生变化
    handleCurrentChange(val){
      this.queryData.pagenum=val;
      this.getGoods()
    },
    // 删除商品
    removeGoods(id){
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
           const {data:res}=await this.$axios.delete(`goods/${id}`)
          if(res.meta.status!=200)return this.$message.error('删除失败')
            this.$message.success('删除成功')
            this.getGoods();
         
        }).catch(() => {
          this.$message('已取消删除');          
        });
    }
    
  }
};
</script>

<style scoped lang='less'>

</style>
